@extends('admin.layout')

@section('content')
<!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                Data Tables
                <small>advanced tables</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                <li><a href="#">Tables</a></li>
                <li class="active">Data tables</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title">Hover Data Table</h3>
                        </div><!-- /.box-header -->
                        <div class="box-body">
                        	<form action="{{ url('admin/user/index') }} ">
	                        <div class="col-sm-8">
		                        <div id="example1_length" class="dataTables_length">
			                        <select name="num" class="form-control input-sm" aria-controls="example1" name="example1_length">
				                        <option @if(!empty($request['num']) && $request['num'] == 10) selected= 'selected' @endif value="10">10</option>
				                        <option @if(!empty($request['num']) && $request['num'] ==25)selected= 'selected'@endif value="25">25</option>
				                        <option @if(!empty($request['num']) && $request['num'] ==50)selected= 'selected'@endif value="50">50</option>
				                        <option @if(!empty($request['num']) && $request['num'] ==100)selected= 'selected'@endif value="100">100</option>
			                        </select>
		                     	</div>
		                    </div>
	                        <div class="col-sm-4">
			                    
                                <div class="input-group input-group-sm">
	                                <input name="keyword" value="{{$request['keyword'] or ''}}" type="text" class="form-control">
				                    <span class="input-group-btn">
				                      <button type="submit" class="btn btn-info btn-flat">搜索</button>
				                    </span>
	                            </div>
                   		 	</div>
							</form>
                            <table id="example2" class="table table-bordered table-hover">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>用户名</th>
                                    <th>邮箱</th>
                                    <th>头像</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                               
                                @foreach($data as $v)
                                <tr>
                                    <td>{{$v -> id}}</td>
                                    <td class="uname">{{$v -> name}}</td>
                                    <td>{{$v -> email}}</td>
                                    <td><img width="20px"src="/uploads/imgs/{{$v -> pic}}" /></td>
                                    <td><a href="{{url('/admin/user/edit')}}/{{$v -> id}}">编辑</a><a href="{{url('/admin/user/delete')}}/{{$v -> id}}">删除</a> </td>
                                </tr>
                                @endforeach
                                </tbody>
                               
                            </table>
                             {{ $data -> appends($request) -> links() }}
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->

                    
                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->
    <script type="text/javascript">
	window.onload = function ()
	{
		$.ajaxSetup({
		    headers: {
		        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		    }
		});
		
		function dbcchangename(){
			var td = $(this);
			var id = $(this).prev().html();
			var oldname = $(this).html();
			var inp = $('<input type="text">');
			inp.val(oldname);
			$(this).html(inp);
			inp.select();
			// console.log($id);
			inp.on('blur', function(){
				var newname = inp.val();
				td.one('dblclick',dbcchangename);
				
				$.ajax({
					type:'post',
					url:"{{ url('/admin/user/changename') }}",
					data:{id:id,name:newname},
					success:function(data)
					{
						if(data == 0)
						{
							alert('用户已存在');
							td.html(oldname);
						}else if(data == 1)
						{
							td.html(newname);
						}else
						{
							td.html(oldname);
						
						}
					},
					error:function()
					{
						alert('异常了');
					},
					datatype:'json'

				});
			});

		}

		$('.uname').one('dblclick',dbcchangename);
	}

	</script>
@endsection
